<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
// require('echarts/theme/macarons') // echarts theme
import moment from 'moment'
import resize from './mixins/resize'
// import { indexPie } from '@/api/index'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    },
    date: ''
  },
  watch: {
    date(newVal, oldVal) {
      this.params.selectDate = newVal
      this.indexPie()
    }
  },
  data() {
    return {
      chart: null,
      count: {
        orderNoPayMoney: 151,
        orderOverdueMoney: 362,
        orderPayMoney: 251,
        orderQualityMoney: 412,
        orderBailMoney: 189,
      },
      params: {
        selectDate: '',
        dateScope: 5,
      }
    }
  },
  created() {
    this.params.selectDate = moment().format('YYYY-MM')
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.indexPie()
    },
    indexPie() {
      // 设置 ECharts 总外框的高度
      this.chart.setOption({
        title: {
          text: '回款情况数据概览',
          textStyle: {
            color: '#999', // 设置标题颜色为深灰色
            fontSize: 14
          }
          // subtext: 'Fake Data'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          bottom: '0%',
          left: 'center'
        },
        height: 500,
        series: [
          {
            name: '',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            padAngle: 5,
            itemStyle: {
              borderRadius: 10,
              color: function (params) {
                // 根据数据项的名称设置不同的颜色
                const colorMap = {
                  '设备': '#91cd76', // 绿色
                  '试剂': '#fac958', // 蓝色
                  '耗材': '#ef6667', // 红色
                };
                return colorMap[params.name];
              }
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: 'bold'
              }
            },
            data: [
              { value: this.count.orderPayMoney, name: '设备' },
              { value: this.count.orderNoPayMoney, name: '试剂' },
              { value: this.count.orderOverdueMoney, name: '耗材' },
            ]
          }
        ]
      })
    }
  }
}
</script>
